#!/bin/bash
# valheim-plus-updater is being called by
# valheim-updater when $VALHEIM_PLUS=true
# It downloads the ValheimPlus mod and merges
# the downloaded archive with the vanilla
# Valheim server into /opt/valheim/plus

# Include defaults and common functions
. /usr/local/etc/valheim/defaults
. /usr/local/etc/valheim/common


main() {
    local api_response
    local download_url
    local remote_updated_at

    if ! api_response=$(curl -sfSL "https://api.github.com/repos/$VALHEIM_PLUS_REPO/releases/$VALHEIM_PLUS_RELEASE"); then
        fatal "Error: could not retrieve ValheimPlus release info from Github API"
    fi
    api_response=$(jq -r ".assets[] | select(.name == \"$vp_zipfile\")" <<< "$api_response" )
    download_url=$(jq -r  ".browser_download_url" <<< "$api_response" )
    remote_updated_at=$(jq -r  ".updated_at" <<< "$api_response" )

    check_for_mod_update \
        "$download_url" \
        "$remote_updated_at" \
        "$vp_zipfile" \
        "$vp_mergefile" \
        "$vp_download_path" \
        "$vp_config_path" \
        "extracted" \
        "$vp_install_path" \
        "ValheimPlus"
}


main
